package com.blog.controller;

import com.blog.entity.RestBean;
import com.blog.entity.user.AccountUser;
import com.blog.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    AccountService service;

    @GetMapping("/me")
    public RestBean<AccountUser> me(@SessionAttribute("account") AccountUser user) {
        return RestBean.success(user);
    }

    @GetMapping("/all")
    public RestBean<List<AccountUser>> all() {
        return RestBean.success(service.getAccountList());
    }

    @GetMapping("/uid")
    public RestBean<AccountUser> all(Integer uid) {
        return RestBean.success(service.getAccountByUid(uid));
    }

    @PostMapping("/reset-password")
    public RestBean<String> restPassword(int uid) {
        String s = service.resetPasswordByUid(uid);
        if (s == null)
            return RestBean.success("密码重置成功");
        else
            return RestBean.failure(400, s);
    }

    @PostMapping("/delete-user")
    public RestBean<String> delete(int uid) {
        String s = service.deleteAccountByUid(uid);
        if (s == null)
            return RestBean.success("用户删除成功");
        else
            return RestBean.failure(400, s);
    }
}
